package com.pengcheng.hnweb.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    private String exchange="hhww";
    private String queue1="hnww.reg";//注册信息队列//给web
    private String queue2="hnww.respon";//回复消息队列
    private String queue3="hnww.logout";//掉线消息队列//给web
    private String queue4="hnww.kickout";//踢出消息队列
    private String queue5="hnww.msg";//消息队列//给web
    private String queue6="hnww.timeout";//消息队列//给文本//2
    private String queue7="hnww.check";//给告警用的//0:msg 1:logout
    @Bean
    public FanoutExchange exchange()
    {
        return new FanoutExchange(exchange);
    }

    @Bean
    public Queue queue1()
    {
        return new Queue(queue1);
    }
    @Bean
    public Queue queue2()
    {
        return new Queue(queue2);
    }
    @Bean
    public Queue queue3()
    {
        return new Queue(queue3);
    }
    @Bean
    public Queue queue4()
    {
        return new Queue(queue4);
    }
    @Bean
    public Queue queue5()
    {
        return new Queue(queue5);
    }
    @Bean
    public Queue queue6()
    {
        return new Queue(queue6);
    }
    @Bean
    public Queue queue7()
    {
        return new Queue(queue7);
    }

    @Bean
    public Binding binding1()
    {
        return BindingBuilder.bind(queue1()).to(exchange());
    }
    @Bean
    public Binding binding4()
    {
        return BindingBuilder.bind(queue4()).to(exchange());
    }
    @Bean
    public Binding binding2()
    {
        return BindingBuilder.bind(queue2()).to(exchange());
    }
    @Bean
    public Binding binding3()
    {
        return BindingBuilder.bind(queue3()).to(exchange());
    }
    @Bean
    public Binding binding5()
    {
        return BindingBuilder.bind(queue5()).to(exchange());
    }
    @Bean
    public Binding binding6()
    {
        return BindingBuilder.bind(queue6()).to(exchange());
    }
    @Bean
    public Binding binding7()
    {
        return BindingBuilder.bind(queue7()).to(exchange());
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setConcurrentConsumers(10);
        factory.setMaxConcurrentConsumers(100);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }
}
